<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Projects extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('students_model', 'projects_model', 'user_profiles_model'));
    }

    function index() {
        if(!in_array($this->tank_auth->get_role(), array('student', 'student_association', 'class_representative')))
            redirect('welcome');

        $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
        $projects = $this->projects_model->get_for_class($class_id);

        foreach($projects as $project)
            $data['projects'][$project->id] = $this->_get_project_data($project);

        $this->ingesup->render('projects/index', $data);
    }

    function view($project_id) {
        $data['project'] = $this->_get_project_data($this->projects_model->get_by_id($project_id));

        if(empty($data['project']))
            redirect('welcome');

        $this->ingesup->render('projects/view', $data);
    }

    function _get_project_data($project) {
        return array(
            'id' => $project->id,
            'teacher_id' => $project->teacher_id,
            'teacher' => $this->user_profiles_model->get_user_fullname($project->teacher_id),
            'start_date' => date('d/m/Y', strtotime($project->start_date)),
            'start_time' => date('H:i:s', strtotime($project->start_date)),
            'end_date' => date('d/m/Y', strtotime($project->end_date)),
            'end_time' => date('H:i:s', strtotime($project->end_date)),
            'title' => $project->title,
            'content' => $project->content
        );
    }
}